package com.elm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.elm.common.R;
import com.elm.dao.UserDao;
import com.elm.dao.requestData.PreData;
import com.elm.dao.requestData.UserSearchData;
import com.elm.domain.User;
import com.elm.utils.FreezeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ResponseBody
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserDao userDao;

    @Autowired
    HttpServletRequest request;

    @Autowired
    HttpServletResponse response;

    @PutMapping
    public R changeInfo(@RequestBody User user)
    {
        System.out.println("收到了数据"+user);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",user.getId());

        int i = userDao.update(user, queryWrapper);
        if(i>0)
        {
            System.out.println("修改成功");
            //修改成功
            return R.success("修改成功");
        }
        System.out.println("修改失败");
        return R.error("修改失败");
    }

    @PostMapping("/getUser")
    public R getUsers(@RequestBody UserSearchData userSearchData)
    {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(!userSearchData.getUserId().equals(""),"id",userSearchData.getUserId())
                .like(!userSearchData.getNickName().equals(""),"nickname",userSearchData.getNickName())
                .like(!userSearchData.getTel().equals(""),"telephone",userSearchData.getTel());

        IPage<User> iPage=new Page<>();
        iPage.setCurrent(userSearchData.getCurrent());
        iPage.setSize(10);

        IPage<User> page = userDao.selectPage(iPage, queryWrapper);

        return R.success(page);
    }

    @PostMapping("/changeStatus")
    public R changeStatus(@RequestBody User user)
    {
        int i = userDao.updateById(user);

        if(user.getStatus()>=20)
        {
            //放入
            boolean b = FreezeUtils.addToPeopleMap(user.getId());
        }
        else
        {
            FreezeUtils.deleteToPeopleMap(user.getId());
        }

        if(i<=0) return R.error("出错了");
        return R.success(user);
    }
}
